package com.stageadminserver.adminserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageadminserver.adminserver.mapper.CategoryMapper;
import com.stageadminserver.adminserver.service.CategoryService;
import com.stagecommon.common.pojo.Category;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;


/**
 * @author: yxl
 * @since: 2021-11-22
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {


    @Override
    public RespBean listSecondCategories() {
        return RespBean.operationSuccess("查找二级分类成功", 200, baseMapper.selectSecondLevelCategories());
    }

    @Override
    public RespBean<IPage<Category>> getAllCategory(Integer currentPage, Integer size, Category category) {
        Page<Category> categoryPage = new Page<>(currentPage, size);
        return RespBean.operationSuccess("查询分类成功", 200, baseMapper.selectAllCategory(categoryPage, category.getCategoryLevel()));
    }

    @Override
    public RespBean<Integer> addCategory(CategoryVo category) {
        if (baseMapper.insertCategory(category.getCategoryName(),category.getCategoryLevel(),category.getParentId()) == 1){
            return RespBean.operationSuccess("添加分类成功！", 200, 1);
        }
        return RespBean.operationFail("添加分类失败！");
    }

    @Override
    public RespBean<Integer> editCategoryName(Category category) {
        if (baseMapper.updateById(category) == 1){
            return RespBean.operationSuccess("修改分类成功！", 200, 1);
        }
        return RespBean.operationFail("修改分类失败！");
    }

    @Override
    public RespBean<Category> selectCategoryById(Category category) {
        return RespBean.operationSuccess("查询成功", 200, baseMapper.selectOne(new QueryWrapper<Category>().select().eq("category_id",category.getCategoryId())));
    }

    @Override
    public RespBean<Integer> deleteCategoryById(Category category) {
        if (baseMapper.deleteById(category.getCategoryId()) == 1){
            return RespBean.operationSuccess("删除成功！", 200, 1);
        } else {
            return RespBean.operationFail("删除失败！");
        }
    }


}
